﻿using MyDAL.helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TPfrom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            initData();
        }

        public void initData()
        {
            Table1.Rows.Clear();
            //表头
            TableRow tr = new TableRow();
            foreach (var item in DataHelper.GetInstance().JobTechnologicalProcesslListHeader)
            {
                TableCell tc = new TableCell();
                tc.Text = item.HeaderName;
                tr.Cells.Add(tc);
            }

            TableCell tc12 = new TableCell();
            tc12.Text = "倒计时";
            tr.Cells.Add(tc12);

            Table1.Rows.Add(tr);
            
            //内容
            int indextr = 0;
            foreach (var item in DataHelper.GetInstance().JobList)
            {
                TableRow tr2 = new TableRow();
                TableCell tc = new TableCell();
                tc.Text = item.JobName;
                tr2.Cells.Add(tc);

                TableCell tc1 = new TableCell();
                tc1.Text = item.JobSubjectCompany;
                tr2.Cells.Add(tc1);

                TableCell tc2 = new TableCell();
                tc2.Text = item.JobAddress;
                tr2.Cells.Add(tc2);

                int indextc = 0;
                foreach (var st in item.JobTechnologicalProcess)
                {
                    Button btnModify = new Button();
                    btnModify.Text = st;
                    btnModify.Click += new EventHandler(btnModify_Click); //添加相应事件
                    btnModify.CommandName = "btnModify_Click";
                    btnModify.CommandArgument = indextr.ToString() + "," + indextc.ToString();


                    btnModify.Enabled = item.JobTProcessEnabled[indextc];
                    //TableCell tc3 = new TableCell();
                    //tc3.Text = st;
                    //tc3.BackColor = ColorTranslator.FromHtml(item.JobTProcessColor[index]);
                    //tr2.Cells.Add(tc3);

                    //String color = ColorTranslator.ToHtml(colorEdit1.Color);

                    TableCell cell = new TableCell();
                    cell.Controls.Add(btnModify);
                    tr2.Cells.Add(cell);
                    indextc++;
                }

                TableCell tc4 = new TableCell();
                tc4.Text = (item.JobStatTime / 60).ToString() + "时" + (item.JobStatTime % 60).ToString()+"分";
                tr2.Cells.Add(tc4);

                TableCell tc5 = new TableCell();
                tc5.Text = item.JobTotalTime.ToString();
                tr2.Cells.Add(tc5);

                if (item.JobTotalTime != 0 && item.JobTProcessEnabled[4])
                {
                    int nowTimeMin = (DateTime.Now.Hour%12)*60 + DateTime.Now.Minute; //当前分钟数
                    int tep = item.JobStatTime - nowTimeMin + item.JobTotalTime;

                    TableCell tc6 = new TableCell();
                    if (tep > 0)
                    {
                        tc6.Text = tep.ToString() + "分";
                    }
                    else
                    {
                        tc6.Text = "已结束";
                    }
                    tr2.Cells.Add(tc6);
                }

                Button btnDel = new Button();
                btnDel.Text = "删除";
                btnDel.Click += new EventHandler(btnDel_Click); //添加相应事件
                btnDel.CommandName = "btnDel_Click";
                btnDel.CommandArgument = indextr.ToString() + "," + indextc.ToString();


                //TableCell tc3 = new TableCell();
                //tc3.Text = st;
                //tc3.BackColor = ColorTranslator.FromHtml(item.JobTProcessColor[index]);
                //tr2.Cells.Add(tc3);

                //String color = ColorTranslator.ToHtml(colorEdit1.Color);

                TableCell celDel = new TableCell();
                celDel.Controls.Add(btnDel);
                tr2.Cells.Add(celDel);


                Table1.Rows.Add(tr2);
                indextr++;
            }
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string[] rc = ((Button)sender).CommandArgument.Split(',');
            int tr = Convert.ToInt32(rc[0]);//获得传递的参数
            int tc = Convert.ToInt32(rc[1]);
            DataHelper.GetInstance().JobList.RemoveAt(tr);
            DataHelper.GetInstance().refrashJobList();
            initData();
        }

            protected void btnModify_Click(object sender, EventArgs e)
        {
            string[] rc = ((Button)sender).CommandArgument.Split(',');
            int tr = Convert.ToInt32(rc[0]);//获得传递的参数
            int tc = Convert.ToInt32(rc[1]);
            if (tc == 0)
            {
                ((Button)sender).Enabled = false;
                //((Button)sender).BackColor = Color.LightGreen;
                DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                Response.Write("<script>alert('车站人员盯控已签认')</script>");
                return;
            }
            if (tc == 1)
            {
                DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                Response.Write("<script>alert('车站值班登记内容已确认')</script>");
                DataHelper.GetInstance().refrashJobList();
                initData();
                return;
            }
            if (tc == 2)
            {
                if (DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[1] == true)
                {
                    Response.Write("<script>alert('先完成前面的流程')</script>");
                    return;
                }
                this.Response.Write("<script language=javascript>window.open('"+ "formStat.aspx?CommandArgument=" + ((Button)sender).CommandArgument + "','newwindow','width=500,height=400,top=250,left=400')</script>");
                //Response.Write("<script>alert('车站人员盯控已签认')</script>");

                DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                DataHelper.GetInstance().refrashJobList();
                initData();
                return;
            }
            if (tc == 3)
            {
                if (DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[2] == true)
                {
                    Response.Write("<script>alert('先完成前面的流程')</script>");
                    return;
                }
                if (DataHelper.GetInstance().JobList[tr].JobNature == "施工")
                {
                    if (DataHelper.GetInstance().JobList[tr].JobLYCPH)
                    {
                        this.Response.Write("<script language=javascript>window.open('for3Yfrom.aspx?CommandArgument=" + ((Button)sender).CommandArgument + "','newwindow','width=400,height=400,top=250,left=400')</script>");
                        DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                        DataHelper.GetInstance().refrashJobList();
                        initData();
                    }
                    else
                    {
                        this.Response.Write("<script language=javascript>window.open('for3Nfrom.aspx?CommandArgument=" + ((Button)sender).CommandArgument + "','newwindow','width=400,height=400,top=250,left=400')</script>");
                        DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                        DataHelper.GetInstance().refrashJobList();
                        initData();
                    }
                }
                else
                {
                    if (DataHelper.GetInstance().JobList[tr].JobLYCPH)
                    {
                        this.Response.Write("<script language=javascript>window.open('for3Yfrom.aspx?CommandArgument=" + ((Button)sender).CommandArgument + "','newwindow','width=400,height=400,top=250,left=400')</script>");
                        DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                        DataHelper.GetInstance().refrashJobList();
                        initData();
                    }
                    else
                    {
                        this.Response.Write("<script language=javascript>window.open('for4Nfrom.aspx?CommandArgument=" + ((Button)sender).CommandArgument + "','newwindow','width=400,height=400,top=250,left=400')</script>");
                        DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                        DataHelper.GetInstance().refrashJobList();
                        initData();
                    }
                }
            }
            if (tc == 4)
            {
                if (DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[3] == true)
                {
                    Response.Write("<script>alert('先完成前面的流程')</script>");
                    return;
                }
                Response.Write("<script>alert('施工结束')</script>");
                DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
                DataHelper.GetInstance().refrashJobList();
                initData();
            }

            //((Button)sender).Enabled = false;
            ////((Button)sender).BackColor = Color.LightGreen;
            //DataHelper.GetInstance().JobList[tr].JobTProcessEnabled[tc] = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormMain.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("TPfrom.aspx");
        }
    }
}